package org.platform.hrv.lib.algorithm;

import org.apache.commons.math3.analysis.interpolation.LinearInterpolator;
import org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction;
import org.apache.commons.math3.complex.Complex;
import org.apache.commons.math3.stat.StatUtils;
import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
import org.apache.commons.math3.stat.descriptive.moment.StandardDeviation;
import org.apache.commons.math3.stat.regression.SimpleRegression;
import org.apache.commons.math3.transform.*;
import org.platform.hrv.lib.hrv.calc.parameter.v2.HRVDataAllProcessor;

import java.util.*;
import java.util.stream.Collectors;

public class AdvancedHRVCalculator {

    public static void main(String[] args) {
        // 输入数据
//        String input = "691,794,755,601,599,1115,661,789,1062,440,638,642,607,1169,578,687,679,753,715,749,639,742,789,416,789,430,694,749,829,795,833,750,830,752,871,610,781,832,789,557,814,789,789,477,1063,618,659,691,700,639,872,811,740,588,511,666,811,811,811,895,566,811,959,637,579,793,693,811,811,671,725,799,507,858,814,811,775,771,1077,471,465,811,811,872,861,1076,760,702,746,663,937,811,464,1138,811,698,621,658,581,859,670,1047,881,811,799,811,811,723,811,623,897,1083,811,1085,811,811,811,811,717,782,762,455,564,1082,781,782,782,624,775,782,782,985,567,717,798,782";

        // topic:{ xunzhong_emo }, partitionId:{ 11 }, offset:{ 10621 } value:{ {"data":[{"lfHfRatio":0.3469071234759784,"nni50":64,"affect":"休眠","stdHr":13.225797439282859,"hr":83,"stressDoc":"比较轻松","mtime":1748501735711,"decode":"体力、精力减退，不想做某些事情","minHr":51.23825789923143,"meanNni":720.6208791208791,"rmssd":155.60947719295632,"uid":"1927981538583257088|861265061204977","meanHr":85.08287021967588,"tiredDef":"当前您会感到不堪重负，觉得十分疲劳。这种状态会使您做事效率低下。建议您可以在休息好的基础上，重新制定目标与计划，以提升效率。这种状态如果长时间累积，可能会影响到您的身体健康，要记得及时调整哦！","sdsd":155.60946306955427,"pnni20":61.87845303867403,"stressDef":"您现在压力较小，不会有太多紧张、焦虑情绪,说明当下没有能伤害或威胁您的现实情况。日常生活中可适当走出舒适区，增加一些挑战，实现最佳压力与身心健康的相互平衡状态。生活方面宜动不宜静，享受热闹的氛围更容易让你保持平静，也可以约上朋友打打球。","medianNni":721.5,"tiredDoc":"疲乏程度较高","stress":8,"maxHr":147.7832512315271,"sdnn":106.39322561461063,"nni20":112,"tired":92,"cvsd":0.21593806355262976,"vlf":539.6449707553053,"emoDef":"当下您的心情非常好，会感到十分愉悦。可能是想到或者发生了令您满意的事情。这种愉悦的状态有利于人身心健康的发展。建议您可以进行一些户外活动或者有效社交。","emotion":76,"lfnu":25.755831076214914,"pnni50":35.35911602209945,"hfnu":74.2441689237851,"lf":1173.972161040607,"cvnni":0.1476410532878328,"emoDoc":"比较愉悦","rangeNni":765,"hf":3384.110851566006,"totalPower":5097.727983361919}]} }
//        String input = "741,753,644,780,732,711,700,680,976,532,829,713,637,406,670,776,814,694,748,462,559,713,472,997,713,713,693,661,745,684,676,710,662,695,695,707,685,681,697,692,721,684,709,720,702,712,695,679,705,713,651,720,734,668,707,955,494,741,733,705,680,693,420,1171,685,528,796,533,1101,1140,845,582,713,527,1102,748,605,665,756,476,650,572,647,713,875,557,713,736,762,749,795,771,778,711,726,761,758,753,727,780,743,742,736,1143,736,686,714,718,745,748,742,787,794,792,730,768,750,755,719,705,740,754,738,718,747,747,750,737,742,740,784,733,703,738,720,731,680,699,718,735,753,745,740,708,710,744,722,724,722,740,745,761,723,717,741,722,705,674,727,707,733,700,730,729,673,770,737,755,745,758,710,731,766,486,670,673,525,673,688,729,710,729";

        String input = "688, 960, 744, 624, 720, 696, 624, 768, 696, 648, 624, 696, 600, 648, 648, 648, 648, 624, 744, 696, 744, 720, 720, 696, 744, 768, 768, 744, 744, 592, 592, 816, 744, 984, 768, 744, 768, 744, 744, 576, 576, 576, 576, 576, 664, 664, 664, 664, 632, 744, 792, 744, 720, 936, 696, 576, 664, 672, 552, 672, 512, 744, 576, 792, 768, 696, 672, 696, 744, 1056, 816, 720, 648, 696, 696, 768, 600, 752, 744, 672, 744, 720, 720, 736, 768, 576, 600, 672, 696, 624, 712, 792, 720, 696, 712, 720, 672, 720, 672, 672, 696, 696, 576, 816, 624, 672, 672, 672, 672, 648, 672, 672, 720, 672, 672, 696, 672, 704, 704, 704";

        /**
         * 时域指标：mean_nni, cvnni, SDNN, range_nni, RMSSD, SDSD, NN50, pNN50, nni20, pnni20, medianNni, mean_hr, min_hr, max_hr, std_hr, cvsd
         *
         * 频域指标：total_power, vlf, lf, hf, lf_hf_ratio, lfnu, hfnu
         *
         * 正确
         * Mean NNI: 720.6208791208791 ms
         * CVNNI: 14.76410532878328 %
         * SDNN: 106.39322561461063 ms
         * Range NNI: 765.0 ms
         * RMSSD: 155.60947719295632 ms
         * NNI50: 64
         * pNN50: 35.35911602209944 %
         * Mean HR: 83.26153423863732 bpm
         * Min HR: 51.23825789923143 bpm
         * Max HR: 147.7832512315271 bpm
         * Std HR: 13.262282467053227 bpm
         * SDSD: 155.61 ms
         * cvsd: 0.215938063552630
         *
         * 不正确
         * Total Power: 4006421.9069316452
         * VLF: 807654.8835918655
         * LF: 1887166.853285726
         * HF: 1311600.1700540537
         * LF/HF Ratio: 1.4388278504172136
         * LFnu: 58.99669589926453 %
         * HFnu: 41.00330410073546 %
         */

        /**
         * 741,753,644,780,732,711,700,680,976,532,829,713,637,406,670,776,814,694,748,462,559,713,472,997,713,713,693,661,745,684,676,710,662,695,695,707,685,681,697,692,721,684,709,720,702,712,695,679,705,713,651,720,734,668,707,955,494,741,733,705,680,693,420,1171,685,528,796,533,1101,1140,845,582,713,527,1102,748,605,665,756,476,650,572,647,713,875,557,713,736,762,749,795,771,778,711,726,761,758,753,727,780,743,742,736,1143,736,686,714,718,745,748,742,787,794,792,730,768,750,755,719,705,740,754,738,718,747,747,750,737,742,740,784,733,703,738,720,731,680,699,718,735,753,745,740,708,710,744,722,724,722,740,745,761,723,717,741,722,705,674,727,707,733,700,730,729,673,770,737,755,745,758,710,731,766,486,670,673,525,673,688,729,710,729
         * 这组数据正确的结果数据
         * {
         *     "data": [
         *         {
         *             "lfHfRatio": 0.3469071234759784,
         *             "nni50": 64,
         *             "affect": "休眠",
         *             "stdHr": 13.225797439282859,
         *             "hr": 83,
         *             "stressDoc": "比较轻松",
         *             "mtime": 1748501735711,
         *             "decode": "体力、精力减退，不想做某些事情",
         *             "minHr": 51.23825789923143,
         *             "meanNni": 720.6208791208791,
         *             "rmssd": 155.60947719295632,
         *             "uid": "1927981538583257088|861265061204977",
         *             "meanHr": 85.08287021967588,
         *             "tiredDef": "当前您会感到不堪重负，觉得十分疲劳。这种状态会使您做事效率低下。建议您可以在休息好的基础上，重新制定目标与计划，以提升效率。这种状态如果长时间累积，可能会影响到您的身体健康，要记得及时调整哦！",
         *             "sdsd": 155.60946306955427,
         *             "pnni20": 61.87845303867403,
         *             "stressDef": "您现在压力较小，不会有太多紧张、焦虑情绪,说明当下没有能伤害或威胁您的现实情况。日常生活中可适当走出舒适区，增加一些挑战，实现最佳压力与身心健康的相互平衡状态。生活方面宜动不宜静，享受热闹的氛围更容易让你保持平静，也可以约上朋友打打球。",
         *             "medianNni": 721.5,
         *             "tiredDoc": "疲乏程度较高",
         *             "stress": 8,
         *             "maxHr": 147.7832512315271,
         *             "sdnn": 106.39322561461063,
         *             "nni20": 112,
         *             "tired": 92,
         *             "cvsd": 0.21593806355262976,
         *             "vlf": 539.6449707553053,
         *             "emoDef": "当下您的心情非常好，会感到十分愉悦。可能是想到或者发生了令您满意的事情。这种愉悦的状态有利于人身心健康的发展。建议您可以进行一些户外活动或者有效社交。",
         *             "emotion": 76,
         *             "lfnu": 25.755831076214914,
         *             "pnni50": 35.35911602209945,
         *             "hfnu": 74.2441689237851,
         *             "lf": 1173.972161040607,
         *             "cvnni": 0.1476410532878328,
         *             "emoDoc": "比较愉悦",
         *             "rangeNni": 765,
         *             "hf": 3384.110851566006,
         *             "totalPower": 5097.727983361919
         *         }
         *     ]
         * }
         */

        List<Integer> rrIntervals = Arrays.stream(input.split(","))
                .map(String::trim)
                .map(Integer::parseInt)
                .collect(Collectors.toList());

        // 计算所有指标
        System.out.println("Mean NNI: " + HRVDataAllProcessor.calculateMeanNNI(rrIntervals) + " ms");
        System.out.println("CVNNI: " + HRVDataAllProcessor.calculateCVNNI(rrIntervals) + " %");
        System.out.println("SDNN: " + HRVDataAllProcessor.calculateSDNN(rrIntervals) + " ms");
        System.out.println("Range NNI: " + HRVDataAllProcessor.calculateRangeNNI(rrIntervals) + " ms");
        System.out.println("RMSSD: " + HRVDataAllProcessor.calculateRMSSD(rrIntervals) + " ms");
        System.out.println("NN50: " + HRVDataAllProcessor.calculateNN50(rrIntervals));
        System.out.println("pNN50: " + HRVDataAllProcessor.calculatePNN50(rrIntervals) + " %");
        double[] nn20Results = HRVDataAllProcessor.calculateNN20(rrIntervals);
        double nn20 = nn20Results[0];
        double pnn20 = nn20Results[1];
        System.out.printf("nni20: %.0f%n", nn20);
        System.out.printf("pnni20: %.2f%%%n", pnn20);
        System.out.println("TINN: " + HRVDataAllProcessor.calculateTINN(rrIntervals) + " ms");
        System.out.println("Triangular Index: " + HRVDataAllProcessor.calculateTriangularIndex(rrIntervals));
        System.out.println("HRV Diff: " + HRVDataAllProcessor.calculateHRVDiff(rrIntervals));
        System.out.println("HRV Ln: " + HRVDataAllProcessor.calculateHRVLn(rrIntervals));
        System.out.println("Mean HR: " + HRVDataAllProcessor.calculateMeanHR(rrIntervals) + " bpm");
        System.out.println("Min HR: " + HRVDataAllProcessor.calculateMinHR(rrIntervals) + " bpm");
        System.out.println("Max HR: " + HRVDataAllProcessor.calculateMaxHR(rrIntervals) + " bpm");
        System.out.println("Std HR: " + HRVDataAllProcessor.calculateStdHR(rrIntervals) + " bpm");
        System.out.printf("SDSD: %.2f ms%n", HRVDataAllProcessor.calculateSDSD(rrIntervals));
        System.out.printf("cvsd: %.15f%n", HRVDataAllProcessor.calculateCVSD(rrIntervals));

        Map<String, Double> spectral = HRVDataAllProcessor.calculateFrequencyDomainMetrics(rrIntervals);
        System.out.println("\nSpectral Analysis:");
        System.out.println("Total Power: " + spectral.get("total_power"));
        System.out.println("VLF: " + spectral.get("VLF"));
        System.out.println("LF: " + spectral.get("LF"));
        System.out.println("HF: " + spectral.get("HF"));
        System.out.println("LF/HF Ratio: " + spectral.get("lf_hf_ratio"));
        System.out.println("LFnu: " + spectral.get("lfnu") + " %");
        System.out.println("HFnu: " + spectral.get("hfnu") + " %");
    }

}